package controllers;

import java.util.List;

import models.Operator;
import models.Oplog;
import models.Role;

import org.apache.commons.lang.StringUtils;

import services.AuthorityService;
import services.RoleService;
import utils.BizConstants;
import utils.Validate;
import vos.RoleSearchVo;
import vos.ReturnCode;
import vos.ReturnListVo;

public class RoleController extends BaseController {
	/**
	 * 角色列表页面
	 *
	 * @Title: roleList 
	 * @return: void
	 */
	public static void roleList(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取角色列表
	 *
	 * @Title: getRole 
	 * @return: void
	 */
	public static void getRole(int page,int rows, String sort,String order,String rolename,String roleid,Boolean isflag){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		RoleSearchVo vo =new RoleSearchVo(page,rows,order,sort,rolename,roleid,isflag);
		long total=RoleService.countRole(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<Role> lists=RoleService.listRole(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 批量启用
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchEnable(String ids){
		if(!checkAuthority("ROLE_ENABLE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=RoleService.betchEnable(ids,oper.operatorid);
		if(issuccess){
			Oplog oplog=new Oplog(oper.operatorid,"批量启用角色："+ids,"角色管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"启用成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"启用失败");
		}
	}
	/**
	 * 批量停用
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchDisable(String ids){
		if(!checkAuthority("ROLE_DISABLE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=RoleService.betchDisable(ids,oper.operatorid);
		if(issuccess){
			Oplog oplog=new Oplog(oper.operatorid,"批量停用角色："+ids,"角色管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"停用成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"停用失败");
		}
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchEnable 
	 * @return: void
	 */
	public static void betchDel(String ids){
		if(!checkAuthority("ROLE_BETCHDEL")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(ids)){
			renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
		}
		boolean issuccess=RoleService.betchDel(ids);
		if(issuccess){
			Operator oper=getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"批量删除角色："+ids,"角色管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 删除记录
	 *
	 * @Title: delRole 
	 * @param id 角色id
	 * @return: void
	 */
	public static void delRole(String id){
		if(!checkAuthority("ROLE_DELETE")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		if(StringUtils.isEmpty(id)){
			renderFailedJson(ReturnCode.ErrorCode,"选中记录不能为空");
		}
		boolean issuccess=RoleService.delRole(id);
		if(issuccess){
			Operator oper=getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"删除角色："+id,"角色管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"删除成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"删除失败");
		}
	}
	/**
	 * 添加角色
	 *
	 * @Title: addRole 
	 * @return: void
	 */
	public static void roleAdd(){
		render();
	}
	/**
	 * 复制角色
	 *
	 * @Title: addRole 
	 * @return: void
	 */
	public static void roleCopy(String id){
		Role oldRole=RoleService.getById(id);
		renderArgs.put("role", oldRole);
		render("RoleController/roleAdd.html");
	}
	/**
	 * 验证角色编号是否存在
	 *
	 * @Title: checkCode 
	 * @param roleid 角色id
	 * @return: void
	 */
	public static void checkCode(String value){
//		if(!Validate.isEnglishNum(value)){
//			renderFailedJson(ReturnCode.ErrorCode, "角色编号格式不正确");
//		}
		if(StringUtils.isEmpty(value)){
			renderFailedJson(ReturnCode.ErrorCode, "编号不能为空");
		}
		boolean isexist=RoleService.checkCode(value);
		if(isexist){
			renderFailedJson(ReturnCode.ErrorCode, "编号已存在");
		}
		else{
			renderFailedJson(ReturnCode.SuccessCode, "该编号可以使用");
		}
	}
	/**
	 * 保存角色信息
	 *
	 * @Title: saveRole 
	 * @param role 角色对象
	 * @return: void
	 */
	public static void saveRole(Role role){
		if(!checkAuthority("ROLE_ADD")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(role==null){
			renderFailedJson(ReturnCode.ErrorCode, "角色不能为空");
		}
		if(StringUtils.isEmpty(role.roleid)){
			renderFailedJson(ReturnCode.ErrorCode, "编号不能为空");
		}
		Role oldRole=RoleService.getById(role.roleid);
		if(oldRole!=null){
			renderFailedJson(ReturnCode.ErrorCode, "角色已存在");
		}
		//验证是不是数字和英语字母组成
//		if(!Validate.isEnglishNum(role.roleid)){
//			renderFailedJson(ReturnCode.ErrorCode, "角色编号格式不正确");
//		}
		role.createname=oper.operatorid;
		boolean isSuccess=RoleService.saveRole(role);
		if(isSuccess){
			Oplog oplog=new Oplog(oper.operatorid,"添加角色："+role.roleid,"角色管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 编辑角色
	 *
	 * @Title: roleEdit 
	 * @return: void
	 */
	public static void roleEdit(String id){
		Role oldRole=RoleService.getById(id);
		renderArgs.put("role", oldRole);
		render();
	}
	/**
	 * 保存角色信息
	 *
	 * @Title: saveRole 
	 * @param role 角色对象
	 * @return: void
	 */
	public static void updateRole(Role role){
		if(!checkAuthority("ROLE_EDIT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		Operator oper=getLoginUser();
		if(role==null){
			renderFailedJson(ReturnCode.ErrorCode, "角色不能为空");
		}
		if(StringUtils.isEmpty(role.roleid)){
			renderFailedJson(ReturnCode.ErrorCode, "编号不能为空");
		}
		Role oldRole=RoleService.getById(role.roleid);
		if(oldRole==null){
			renderFailedJson(ReturnCode.ErrorCode, "角色不存在");
		}
		role.modifyname=oper.operatorid;
		boolean isSuccess=RoleService.updateRole(role);
		if(isSuccess){
			Oplog oplog=new Oplog(oper.operatorid,"编辑角色："+role.roleid,"角色管理");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode, "保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode, "保存失败");
		}
	}
	/**
	 * 获取子表中用户信息
	 *
	 * @Title: getOperator 
	 * @return: void
	 */
	public static void getOperator(int page,int rows,String order,String sort,String roleid){
		if(StringUtils.isEmpty(roleid)){
			renderJSON(null);
		}
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		RoleSearchVo vo =new RoleSearchVo(page,rows,order,sort,roleid);
		long total=RoleService.countOperator(roleid);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<Operator> opers=RoleService.getOperator(vo);
		listVo.setRows(opers);
		renderJSON(listVo);
	}
}
